var UIPanelManager = cc.Class({
    statics:{

        //界面ui
        settingNode:"settingNode",
        sideBarReward:"sideBarReward",
        signNode:"signNode",
        gradeNode:"gradeNode",
        matchNode:"matchNode",
        levelNode:"levelNode",
        matchFriendNode:"matchFriendNode",
        resultNode:"resultNode",

        instances : {},
        assets : {},

         /**
     * 打开一个UI
     * @param {string} uiName UI名称
     * @param {Function} callback 回调函数
     */
    async open(uiName, callback = null) {
        if (!uiName) {
            console.error("UI name is required.");
            return;
        }

        const arr = uiName.split('/');
        const cleanUiName = arr[arr.length - 1];
        console.log("打开面板  " + cleanUiName);

        try {
            let node = UIPanelManager.instances[cleanUiName];
            if (node) {
                node.active = true;
                UIPanelManager.reshow(node, cleanUiName);
                callback && callback(undefined, node);
                return node;
            }

            const asset = UIPanelManager.assets[cleanUiName] || await UIPanelManager.loadResPanel(`ui/${uiName}`, cc.Prefab);
            UIPanelManager.assets[cleanUiName] = asset;
            node = cc.instantiate(asset);
            UIPanelManager.reshow(node, cleanUiName);
            UIPanelManager.instances[cleanUiName] = node;
            callback && callback(undefined, node);
            return node;
        } catch (error) {
            callback ? callback(error) : console.error(error);
        }
    },

    /**
     * 关闭一个UI
     * @param {string|cc.Node} uiName UI名称 | UI节点对象
     * @param {boolean} isDestroy 是否销毁节点
     */
    close(uiName, isDestroy = false) {
        let node;
        let cleanUiName;

        if (typeof uiName === 'string') {
            const arr = uiName.split('/');
            cleanUiName = arr[arr.length - 1];
            node = UIPanelManager.instances[cleanUiName];
        } else {
            node = uiName;
            cleanUiName = node.name;
        }

        if (!node) {
            console.warn("Node not found for:", cleanUiName);
            return null;
        }

        node.active = false;
        if (isDestroy) {
            UIPanelManager.instances[cleanUiName] = undefined;
            node.destroy();
        } else {
            node.parent = null; // 如果需要隐藏而不是销毁，可以移除父节点
        }

        return isDestroy ? null : node;
    },

    /**
     * 加载动态资源
     * @param {string} path 资源路径
     * @param {cc.Asset} assetType 资源类型
     */
    loadRes(path, assetType) {
        return new Promise((resolve, reject) => {
            cc.resources.load(path, assetType, (err, res) => {
                if (err) {
                    return reject(err);
                }
                return resolve(res);
            });
        });
    },

    /**
     * 加载Panel资源
     * @param {string} uiName Panel资源路径
     * @param {cc.Asset} assetType 资源类型
     */
    async loadResPanel(uiName, assetType) {
        const arr = uiName.split('/');
        const cleanUiName = arr[arr.length - 1];

        if (UIPanelManager.assets[cleanUiName]) {
            return UIPanelManager.assets[cleanUiName];
        }
        UIPanelManager.assets[cleanUiName] = await UIPanelManager.loadRes(uiName, assetType);

        return UIPanelManager.assets[cleanUiName];
    },

    /** 重置节点层级和父节点 */
    reshow(node, uiName) {
        const canvas = cc.director.getScene().getChildByName('Canvas').getChildByName("popUi");
        if (canvas) {
            node.parent = canvas;
            // 使用 setSiblingIndex 来调整层级
            //node.setSiblingIndex(0); // 根据需要调整层级
        } else {
            console.warn("Canvas node not found.");
        }
    },

    
    },
})

module.exports = UIPanelManager
